package com.vpen.client.stomp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/11/28 21:58
 */
@Slf4j
@Component
public class ClientStompSessionHandler extends StompSessionHandlerAdapter {


    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
        log.debug("成功连接后....");
        log.debug("做一些成功后的逻辑");
    }

    @Override
    public Type getPayloadType(StompHeaders headers) {
        log.debug("getPayloadType:{}", headers);
        return super.getPayloadType(headers);
    }

    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        log.debug("处理服务器发来的消息：{}", payload);
        log.debug("保存到数据库.....");
    }


    @Override
    public void handleException(StompSession session,
                                StompCommand command,
                                StompHeaders headers,
                                byte[] payload,
                                Throwable exception) {
        log.error("处理异常:{}", exception.getMessage());
    }

    @Override
    public void handleTransportError(StompSession session, Throwable exception) {
        log.error("处理传输异常：{}", exception.getMessage());
    }
}
